function setWatcher(page) {
    let data = page.data;
    let watch = page.watch;

    let nowData = data; //{val: "", obj: {…}, __webviewId__: 5}

    Object.keys(watch).forEach((v) => {
        // v: val   v: obj
        let key = v.split("."); // 1:["val"] 2:["obj"]
        for (let i = 0; i < key.length - 1; i++) {
            nowData = nowData[key[i]];
        }
        // lastKey => key[0] => val
        let lastKey = key[key.length - 1];
        /**
                                                                                                                 *  ƒ val(v) {
                                                                                                                        console.log(v);
                                                                                                                    }
                                                                                                                    ƒ handler(v) {
                                                                                                                        console.log(v);
                                                                                                                    } 
                                                                                                                    */
        let watchFun = watch[v].handler || watch[v];
        // console.log(watchFun);

        let deep = watch[v].deep;
        // console.log(deep); //1: undefined 2: true

        observe(nowData, lastKey, watchFun, deep, page);
    });
}

function observe(obj, key, watchFun, deep, page) {
    console.log(key);

    var val = obj[key];
    console.log(obj);

    if (deep && val != null && typeof val === "object") {
        console.log("满足条件");

        Object.keys(val).forEach((childKey) => {
            console.log("childKey", childKey);

            observe(val, childKey, watchFun, deep, page);
        });
    }

    Object.defineProperty(obj, key, {
        configurable: true,
        enumerable: true,
        set: function(newVal) {
            watchFun.call(page, newVal, val);
            val = newVal;
            if (deep) {
                observe(obj, key, watchFun, deep, page);
            }
        },
        get: function() {
            return val;
        },
    });
}

module.exports = {
    setWatcher: setWatcher,
};